<template>
  <header class="header">
    <!-- 头部第一行 -->
    <div class="top">
      <div class="container">
        <div class="typeList">
          <router-link to="/myIndent">我的订单</router-link>
          <router-link to="/collection">我的收藏</router-link>
          <span>
            <router-link to="/shopCart" class="cart">
              <i class="el-icon-shopping-cart-full"></i>
              <i class="car">购物车</i>
              <i :class="{active:shopSum>0}">({{shopSum}})</i>
            </router-link>
          </span>
        </div>

        <div class="loginList" >
          <!-- 没登录 -->
          <p v-if="!userName" >
            <router-link to="/login" >登录</router-link>
            <router-link to="/register" class="register">注册</router-link>
          </p>
          <p v-else>
                <a>{{userName}}</a>
                <a class="register" @click="quitLogin">退出登录</a>
            </p>
        </div>
      </div>
    </div>

    <!-- 头部第二行 -->
    <div class="bottom">
      <!-- logo图部分 -->
      <h1 class="logoArea">
        <router-link to="/home" class="logo">
          <img src="http://106.15.179.105/img/logo.c8999c02.png"  />
        </router-link>
        <!-- <a href="##" class="logo">
          <img src="http://106.15.179.105/img/logo.c8999c02.png"  />
        </a> -->
      </h1>
      <!-- 文字部分 -->
      <div class="textArea">
        <router-link :class="{active:navId===0}" @click.native="changeNavId(0)" to="/home">首页</router-link>
        <router-link :class="{active:navId===1}" @click.native="changeNavId(1)" to="/goods">全部商品</router-link>
        <a :class="{active:navId===2}" @click="changeNavId(2)" href="##">关于我们</a>
      </div>
      <!-- 搜索框区域 -->
      <div class="searchArea" style="margin-top: 15px">
        <el-input
          placeholder="请输入搜索内容"
          v-model="keyword"
          class="input-with-select"
        >
        <!-- @click="goGoods" -->
          <el-button slot="append"  icon="el-icon-search" @click="gosearch"></el-button>
        </el-input>
      </div>
    </div>
  </header>
</template>

<script>
import {reqShopCart} from "@/api"
export default {
  name: "Header",
  data() {
    return {
      keyword:"",
      shopSum:localStorage.getItem("shopSum"),
      navId:0,
    };
  },
  methods: {
    goGoods(){
      // 代表如果有query参数传过去
      if(this.$route.query){
        let location = {
          name:'search',
          params:{keyword:this.keyword || undefined}
        };
        location.query = this.$route.query;
        this.$router.path(location)
      }
    },
    // 退出登录
    quitLogin(){
      try{
       this.$store.dispatch("quitLogin")
      this.$router.push("/home")
     }catch(e){
       alert("退出失败")
     }
    },
    // 获取购物车数据
    // async getShopCart(){
    //   // let result = await reqShopCart()
    //   // if(result.data[0]){
    //   //   this.shopSum = result.data[0].cartInfoList.length
    //   // }
    //   this.$nextTick(()=>{
    //     this.shopSum = localStorage.getItem("shopSum")
    //   })

    // },
    // 动态添加类名
    changeNavId(navId){
      this.navId = navId
    },
    // 点击搜索按钮
    gosearch(){
      // this.$message({
      //   message: '系统正在升级,请稍后',
      //   type: 'warning'
      // });
      let location={
        name:"goods",
        params:{keyword:this.keyword || undefined}
      }
      this.$router.push(location);
    }

  },
  
  mounted() {
    // 通过全局事件总线清除关键字
    // this.$bus.$on('clear',()=> {
    //   this.keyword = ""
    // })
    this.$nextTick(()=>{
      this.getShopCart()
    })
    // 通过全局事件总线清除关键字
    this.$bus.$on("clear",()=>{
      this.keyword=''
    })
  },
  // 获取用户信息
  computed:{
    userName(){
      return this.$store.state.user.usetInfo.name;
    },
    // shopSum(){
    //   return localStorage.getItem("shopSum")
    // }
  },
  watch:{
    $route:{
      immediate:true,
      handler(){
        this.shopSum = localStorage.getItem("shopSum")
      }
    }
  }
};
</script>

<style scoped lang="less">
.header {
    
  // 头部样式
  & > .top {
    padding-right: 10px;
    background-color: #3d3d3d;
    height: 40px;
    line-height: 40px;
    .container {
      width: 1220px;
      margin: 0 auto;
      overflow: hidden;
      .loginList {
        float: right;

        p {
          a {
            font-size: 14px;
            color: #b0b0b0;
          }
          .register {
            border-left: 1px solid #b3aeae;
            padding: 0 5px;
            margin-left: 5px;
          }
        }
      }
      .loginList:hover{
        color: #fff !important;
      }
      .typeList {
        a {
          color: #b0b0b0;
          padding: 0 10px;
        }
        padding-left: 10px;
        float: right;
        font-size: 14px;
        line-height: 40px;
        text-align: center;
        .cart {
          .car {
            padding: 0 5px;
          }
          .active{
            color: #ff6700;
          }
          display: inline-block;
          width: 100px;
          // background-color: chartreuse;
          i {
            font-size: 14px;
            color: #b0b0b0;
          }
        }
        .cart:hover {
          display: inline-block;
          background-color: #fff;
          color: #ff6700 !important;
          cursor: pointer;
        }
      }
    }
  }
  //第二行样式
  & > .bottom {
    border-bottom: 1px solid #EAECEF;
    width: 1220px;
    margin: 0 auto;
    overflow: hidden;
    //   background-color: pink;
    .logoArea {
      float: left;
      .logo {
        img {
          width: 189px;
          margin: 15px 10px 0 0;
        }
      }
    }
    .textArea {
      & .active{
          color: #409EFF;
          padding-bottom: 23px;
          border-bottom: 2px solid #409EFF;
          // text-decoration: underline;
        
      }
      float: left;
      margin: 25px 90px 0 90px;
      a {
        font-size: 14px;
        color: #b0b0b0;
        line-height: 40px;
        padding: 0 20px;
      }
      a:hover {
        color: #111 !important;
      }
    }
    .el-input {
        margin-top: 15px;
        float: right;
        width: 300px;
    }
    
  }
}
</style>